﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MassSpring
{
    public class JelloCube : IComparable<JelloCube>
    {
        private static int unitsPerTexCoord = 10;

        public Color color;
        public Game1 game;
        public int xnodes, ynodes, znodes;
        public Vector3 position;
        public float xEdgeLength, yEdgeLength, zEdgeLength;
        public PhysicsNode[, ,] cubeNodes;
        public Game1.JelloVertexFormat[] cubeVertices;
        public Matrix rotation;

        public RenderTarget2D jelloDepthTarget;

        public JelloCube(Color color, Game1 game, Vector3 position, Matrix rotation, int xnodes = 8, int ynodes = 8, int znodes = 8, float xlength = 1, float ylength = 1, float zlength = 1)
        {
            this.color = color;
            this.game = game;
            this.xnodes = xnodes;
            this.ynodes = ynodes;
            this.znodes = znodes;
            this.xEdgeLength = xlength;
            this.yEdgeLength = ylength;
            this.zEdgeLength = zlength;
            this.position = position;
            this.rotation = rotation;
        }

        public void init()
        {
            initCube();
            initCubeVerts();
        }

        public void loadContent()
        {
            jelloDepthTarget = new RenderTarget2D(game.GraphicsDevice, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height,
                false, SurfaceFormat.Single, DepthFormat.Depth24Stencil8);
        }

        private void initCube()
        {
            const float CUBESTRUCTURALSPRINGCONSTANT = 50;
            const float CUBESHEARSPRINGCONSTANT = 50;
            const float CUBEFLEXIONSPRINGCONSTANT = 15;
            const float CUBEEDGEDAMPING = 0.01f;
            const float CUBEPARTICLEMASS = 0.1f;
            cubeNodes = new PhysicsNode[xnodes, ynodes, znodes];

            Vector3 cubeCenter = new Vector3(position.X + (float)xnodes * xEdgeLength / 2,
                position.Y + (float)ynodes * yEdgeLength / 2,
                position.Z + (float)znodes * zEdgeLength / 2);

            //Create nodes
            for (int i = 0; i < xnodes; i++)
                for (int j = 0; j < ynodes; j++)
                    for (int k = 0; k < znodes; k++)
                    {
                        PhysicsNode p = new PhysicsNode();
                        Vector3 positionBeforeRot = new Vector3(position.X + i * xEdgeLength, position.Y + j * yEdgeLength, position.Z + k * zEdgeLength);
                        //Transform to center
                        positionBeforeRot = positionBeforeRot - cubeCenter;
                        positionBeforeRot = Vector3.Transform(positionBeforeRot, rotation);
                        positionBeforeRot = positionBeforeRot + cubeCenter;
                        p.Position = positionBeforeRot;
                        p.Mass = CUBEPARTICLEMASS;
                        p.Damping = 0.8f;
                        game.particleSystem.AddNode(p);
                        cubeNodes[i, j, k] = p;
                    }

            //Add structural spring in all three dimensions, X,Y,Z
            //X Direction
            for (int i = 0; i < xnodes - 1; i++)
                for (int j = 0; j < ynodes; j++)
                    for (int k = 0; k < znodes; k++)
                    {
                        Spring spr = new Spring(CUBESTRUCTURALSPRINGCONSTANT, CUBEEDGEDAMPING, xEdgeLength);
                        spr.Attach(cubeNodes[i, j, k], cubeNodes[i + 1, j, k]);
                        game.particleSystem.AddSpring(spr);
                    }
            //Y
            for (int i = 0; i < xnodes; i++)
                for (int j = 0; j < ynodes - 1; j++)
                    for (int k = 0; k < znodes; k++)
                    {
                        Spring spr = new Spring(CUBESTRUCTURALSPRINGCONSTANT, CUBEEDGEDAMPING, yEdgeLength);
                        spr.Attach(cubeNodes[i, j, k], cubeNodes[i, j + 1, k]);
                        game.particleSystem.AddSpring(spr);
                    }

            //Z
            for (int i = 0; i < xnodes; i++)
                for (int j = 0; j < ynodes; j++)
                    for (int k = 0; k < znodes - 1; k++)
                    {
                        Spring spr = new Spring(CUBESTRUCTURALSPRINGCONSTANT, CUBEEDGEDAMPING, zEdgeLength);
                        spr.Attach(cubeNodes[i, j, k], cubeNodes[i, j, k + 1]);
                        game.particleSystem.AddSpring(spr);
                    }

            //Add shear spring for each node (+X+Y, +X+Z, +Y+Z, -X+Y, -X+Z, -Y+Z, +X+Y+Z, -X+Y+Z, +X-Y+Z, +X-Y-Z ) 
            //+X+Y
            for (int i = 0; i < xnodes - 1; i++)
                for (int j = 0; j < ynodes - 1; j++)
                    for (int k = 0; k < znodes; k++)
                    {
                        Spring spr = new Spring(CUBESHEARSPRINGCONSTANT, CUBEEDGEDAMPING, (float)Math.Sqrt((float)Math.Pow(xEdgeLength, 2) + (float)Math.Pow(yEdgeLength, 2)));
                        spr.Attach(cubeNodes[i, j, k], cubeNodes[i + 1, j + 1, k]);
                        game.particleSystem.AddSpring(spr);
                    }
            //+X+Z
            for (int i = 0; i < xnodes - 1; i++)
                for (int j = 0; j < ynodes; j++)
                    for (int k = 0; k < znodes - 1; k++)
                    {
                        Spring spr = new Spring(CUBESHEARSPRINGCONSTANT, CUBEEDGEDAMPING, (float)Math.Sqrt((float)Math.Pow(xEdgeLength, 2) + (float)Math.Pow(zEdgeLength, 2)));
                        spr.Attach(cubeNodes[i, j, k], cubeNodes[i + 1, j, k + 1]);
                        game.particleSystem.AddSpring(spr);
                    }
            //+Y+Z
            for (int i = 0; i < xnodes; i++)
                for (int j = 0; j < ynodes - 1; j++)
                    for (int k = 0; k < znodes - 1; k++)
                    {
                        Spring spr = new Spring(CUBESHEARSPRINGCONSTANT, CUBEEDGEDAMPING, (float)Math.Sqrt((float)Math.Pow(yEdgeLength, 2) + (float)Math.Pow(zEdgeLength, 2)));
                        spr.Attach(cubeNodes[i, j, k], cubeNodes[i, j + 1, k + 1]);
                        game.particleSystem.AddSpring(spr);
                    }
            //-X+Y
            for (int i = 1; i < xnodes; i++)
                for (int j = 0; j < ynodes - 1; j++)
                    for (int k = 0; k < znodes; k++)
                    {
                        Spring spr = new Spring(CUBESHEARSPRINGCONSTANT, CUBEEDGEDAMPING, (float)Math.Sqrt((float)Math.Pow(xEdgeLength, 2) + (float)Math.Pow(yEdgeLength, 2)));
                        spr.Attach(cubeNodes[i, j, k], cubeNodes[i - 1, j + 1, k]);
                        game.particleSystem.AddSpring(spr);
                    }
            //-X+Z
            for (int i = 1; i < xnodes; i++)
                for (int j = 0; j < ynodes; j++)
                    for (int k = 0; k < znodes - 1; k++)
                    {
                        Spring spr = new Spring(CUBESHEARSPRINGCONSTANT, CUBEEDGEDAMPING, (float)Math.Sqrt((float)Math.Pow(xEdgeLength, 2) + (float)Math.Pow(zEdgeLength, 2)));
                        spr.Attach(cubeNodes[i, j, k], cubeNodes[i - 1, j, k + 1]);
                        game.particleSystem.AddSpring(spr);
                    }
            //-Y+Z
            for (int i = 0; i < xnodes; i++)
                for (int j = 1; j < ynodes; j++)
                    for (int k = 0; k < znodes - 1; k++)
                    {
                        Spring spr = new Spring(CUBESHEARSPRINGCONSTANT, CUBEEDGEDAMPING, (float)Math.Sqrt((float)Math.Pow(yEdgeLength, 2) + (float)Math.Pow(zEdgeLength, 2)));
                        spr.Attach(cubeNodes[i, j, k], cubeNodes[i, j - 1, k + 1]);
                        game.particleSystem.AddSpring(spr);
                    }
            //+X+Y+Z
            for (int i = 0; i < xnodes - 1; i++)
                for (int j = 0; j < ynodes - 1; j++)
                    for (int k = 0; k < znodes - 1; k++)
                    {
                        Spring spr = new Spring(CUBESHEARSPRINGCONSTANT, CUBEEDGEDAMPING, (float)Math.Sqrt((float)Math.Pow(xEdgeLength, 2) + (float)Math.Pow(yEdgeLength, 2) + (float)Math.Pow(zEdgeLength, 2)));
                        spr.Attach(cubeNodes[i, j, k], cubeNodes[i + 1, j + 1, k + 1]);
                        game.particleSystem.AddSpring(spr);
                    }
            //-X+Y+Z
            for (int i = 1; i < xnodes; i++)
                for (int j = 0; j < ynodes - 1; j++)
                    for (int k = 0; k < znodes - 1; k++)
                    {
                        Spring spr = new Spring(CUBESHEARSPRINGCONSTANT, CUBEEDGEDAMPING, (float)Math.Sqrt((float)Math.Pow(xEdgeLength, 2) + (float)Math.Pow(yEdgeLength, 2) + (float)Math.Pow(zEdgeLength, 2)));
                        spr.Attach(cubeNodes[i, j, k], cubeNodes[i - 1, j + 1, k + 1]);
                        game.particleSystem.AddSpring(spr);
                    }
            //+X-Y+Z
            for (int i = 0; i < xnodes - 1; i++)
                for (int j = 1; j < ynodes; j++)
                    for (int k = 0; k < znodes - 1; k++)
                    {
                        Spring spr = new Spring(CUBESHEARSPRINGCONSTANT, CUBEEDGEDAMPING, (float)Math.Sqrt((float)Math.Pow(xEdgeLength, 2) + (float)Math.Pow(yEdgeLength, 2) + (float)Math.Pow(zEdgeLength, 2)));
                        spr.Attach(cubeNodes[i, j, k], cubeNodes[i + 1, j - 1, k + 1]);
                        game.particleSystem.AddSpring(spr);
                    }
            //+X-Y-Z
            for (int i = 0; i < xnodes - 1; i++)
                for (int j = 1; j < ynodes; j++)
                    for (int k = 1; k < znodes; k++)
                    {
                        Spring spr = new Spring(CUBESHEARSPRINGCONSTANT, CUBEEDGEDAMPING, (float)Math.Sqrt((float)Math.Pow(xEdgeLength, 2) + (float)Math.Pow(yEdgeLength, 2) + (float)Math.Pow(zEdgeLength, 2)));
                        spr.Attach(cubeNodes[i, j, k], cubeNodes[i + 1, j - 1, k - 1]);
                        game.particleSystem.AddSpring(spr);
                    }

            //Add flexion springs X, Y, Z
            //X Direction
            for (int i = 0; i < xnodes - 2; i++)
                for (int j = 0; j < ynodes; j++)
                    for (int k = 0; k < znodes; k++)
                    {
                        Spring spr = new Spring(CUBEFLEXIONSPRINGCONSTANT, CUBEEDGEDAMPING, 2 * xEdgeLength);
                        spr.Attach(cubeNodes[i, j, k], cubeNodes[i + 2, j, k]);
                        game.particleSystem.AddSpring(spr);
                    }
            //Y
            for (int i = 0; i < xnodes; i++)
                for (int j = 0; j < ynodes - 2; j++)
                    for (int k = 0; k < znodes; k++)
                    {
                        Spring spr = new Spring(CUBEFLEXIONSPRINGCONSTANT, CUBEEDGEDAMPING, 2 * yEdgeLength);
                        spr.Attach(cubeNodes[i, j, k], cubeNodes[i, j + 2, k]);
                        game.particleSystem.AddSpring(spr);
                    }

            //Z
            for (int i = 0; i < xnodes; i++)
                for (int j = 0; j < ynodes; j++)
                    for (int k = 0; k < znodes - 2; k++)
                    {
                        Spring spr = new Spring(CUBEFLEXIONSPRINGCONSTANT, CUBEEDGEDAMPING, 2 * zEdgeLength);
                        spr.Attach(cubeNodes[i, j, k], cubeNodes[i, j, k + 2]);
                        game.particleSystem.AddSpring(spr);
                    }
        }

        private void initCubeVerts()
        {
            int numCubeVerts =
                (cubeNodes.GetLength(0) - 1) * (cubeNodes.GetLength(1) - 1) * 6 * 2 +
                (cubeNodes.GetLength(1) - 1) * (cubeNodes.GetLength(2) - 1) * 6 * 2 +
                (cubeNodes.GetLength(2) - 1) * (cubeNodes.GetLength(0) - 1) * 6 * 2;
            cubeVertices = new Game1.JelloVertexFormat[numCubeVerts];

            //Only draw outsides
            //Set verts side 1
            int vertIndex = 0;
            for (int i = 0; i < cubeNodes.GetLength(0) - 1; i++)
                for (int j = 0; j < cubeNodes.GetLength(1) - 1; j++)
                {
                    int k = 0;
                    Vector3 n1, n2, n3, n4;
                    n1 = cubeNodes[i, j, k].Position;
                    n2 = cubeNodes[i, j + 1, k].Position;
                    n3 = cubeNodes[i + 1, j, k].Position;
                    n4 = cubeNodes[i + 1, j + 1, k].Position;
                    Vector2 t1, t2, t3, t4;
                    float floati = (float)i; float floatj = (float)j;
                    float l1 = (float)(cubeNodes.GetLength(0) - 1); float l2 = (cubeNodes.GetLength(1) - 1);
                    float TexcoordMult1 = l1 / unitsPerTexCoord * xEdgeLength;
                    float TexcoordMult2 = l2 / unitsPerTexCoord * yEdgeLength;
                    t1 = new Vector2(floati / l1 * TexcoordMult1, floatj / l2 * TexcoordMult2);
                    t2 = new Vector2(floati / l1 * TexcoordMult1, (floatj + 1.0f) / l2 * TexcoordMult2);
                    t3 = new Vector2((floati + 1.0f) / l1 * TexcoordMult1, floatj / l2 * TexcoordMult2);
                    t4 = new Vector2((floati + 1.0f) / l1 * TexcoordMult1, (floatj + 1.0f) / l2* TexcoordMult2);
                    Vector3 normal = Vector3.Cross(n2 - n1, n3 - n1);
                    Vector3 tan1, tan2, tan3, tan4;
                    //Tangent in the direction of the U coordinates
                    tan1 = Vector3.Normalize(n3 - n1);
                    tan2 = Vector3.Normalize(n4 - n2);
                    tan3 = tan1;
                    tan4 = tan2;
                    Vector3 bin1, bin2, bin3, bin4;
                    //Binormal in the direction of the V coordinates.
                    bin1 = Vector3.Normalize(n2 - n1);
                    bin2 = bin1;
                    bin3 = Vector3.Normalize(n4 - n3);
                    bin4 = bin3;

                    cubeVertices[vertIndex] = new Game1.JelloVertexFormat(n1, normal, color, t1, tan1, bin1);
                    cubeVertices[vertIndex + 1] = new Game1.JelloVertexFormat(n3, normal, color, t3, tan3, bin3);
                    cubeVertices[vertIndex + 2] = new Game1.JelloVertexFormat(n2, normal, color, t2, tan2, bin2);

                    normal = Vector3.Cross(n3 - n4, n2 - n4);
                    cubeVertices[vertIndex + 3] = new Game1.JelloVertexFormat(n2, normal, color, t2, tan2, bin2);
                    cubeVertices[vertIndex + 4] = new Game1.JelloVertexFormat(n3, normal, color, t3, tan3, bin3);
                    cubeVertices[vertIndex + 5] = new Game1.JelloVertexFormat(n4, normal, color, t4, tan4, bin4);
                    vertIndex += 6;
                }

            //Set verts side 1opp
            for (int i = 0; i < cubeNodes.GetLength(0) - 1; i++)
                for (int j = 0; j < cubeNodes.GetLength(1) - 1; j++)
                {
                    int k = cubeNodes.GetLength(2) - 1;
                    Vector3 n1, n2, n3, n4;
                    n1 = cubeNodes[i, j, k].Position;
                    n2 = cubeNodes[i, j + 1, k].Position;
                    n3 = cubeNodes[i + 1, j, k].Position;
                    n4 = cubeNodes[i + 1, j + 1, k].Position;
                    Vector3 normal = Vector3.Cross(n3 - n1, n2 - n1);
                    Vector2 t1, t2, t3, t4;
                    float floati = (float)i; float floatj = (float)j;
                    float l1 = (float)(cubeNodes.GetLength(0) - 1); float l2 = (cubeNodes.GetLength(1) - 1);
                    float TexcoordMult1 = l1 / unitsPerTexCoord * xEdgeLength;
                    float TexcoordMult2 = l2 / unitsPerTexCoord * yEdgeLength;
                    t1 = new Vector2(floati / l1 * TexcoordMult1, 1 - (floatj / l2 * TexcoordMult2));
                    t2 = new Vector2(floati / l1 * TexcoordMult1, 1 - ((floatj + 1.0f) / l2 * TexcoordMult2));
                    t3 = new Vector2((floati + 1.0f) / l1 * TexcoordMult1, 1 - (floatj / l2 * TexcoordMult2));
                    t4 = new Vector2((floati + 1.0f) / l1 * TexcoordMult1, 1 - ((floatj + 1.0f) / l2 * TexcoordMult2));

                    Vector3 tan1, tan2, tan3, tan4;
                    //Tangent in the direction of the U coordinates
                    tan1 = Vector3.Normalize(n3 - n1);
                    tan2 = Vector3.Normalize(n4 - n2);
                    tan3 = tan1;
                    tan4 = tan2;
                    Vector3 bin1, bin2, bin3, bin4;
                    //Binormal in the direction of the V coordinates.
                    bin1 = Vector3.Normalize(n2 - n1);
                    bin2 = bin1;
                    bin3 = Vector3.Normalize(n4 - n3);
                    bin4 = bin3;

                    cubeVertices[vertIndex] = new Game1.JelloVertexFormat(n1, normal, color, t1, tan1, bin1);
                    cubeVertices[vertIndex + 1] = new Game1.JelloVertexFormat(n2, normal, color, t2, tan2, bin2);
                    cubeVertices[vertIndex + 2] = new Game1.JelloVertexFormat(n3, normal, color, t3, tan3, bin3);

                    normal = Vector3.Cross(n2 - n4, n3 - n4);
                    cubeVertices[vertIndex + 3] = new Game1.JelloVertexFormat(n3, normal, color, t2, tan2, bin2);
                    cubeVertices[vertIndex + 4] = new Game1.JelloVertexFormat(n2, normal, color, t3, tan3, bin3);
                    cubeVertices[vertIndex + 5] = new Game1.JelloVertexFormat(n4, normal, color, t4, tan4, bin4);
                    vertIndex += 6;
                }

            //2
            for (int j = 0; j < cubeNodes.GetLength(1) - 1; j++)
                for (int k = 0; k < cubeNodes.GetLength(2) - 1; k++)
                {
                    int i = 0;
                    Vector3 n1, n2, n3, n4;
                    n1 = cubeNodes[i, j, k].Position;
                    n2 = cubeNodes[i, j, k + 1].Position;
                    n3 = cubeNodes[i, j + 1, k].Position;
                    n4 = cubeNodes[i, j + 1, k + 1].Position;
                    Vector3 normal = Vector3.Cross(n2 - n1, n3 - n1);
                    Vector2 t1, t2, t3, t4;
                    float floati = (float)j; float floatj = (float)k;
                    float l1 = (float)(cubeNodes.GetLength(1) - 1); float l2 = (cubeNodes.GetLength(2) - 1);
                    float TexcoordMult1 = l1 / unitsPerTexCoord * yEdgeLength;
                    float TexcoordMult2 = l2 / unitsPerTexCoord * zEdgeLength;
                    t1 = new Vector2(1 - (floati / l1 * TexcoordMult1), 1 - (floatj / l2 * TexcoordMult2));
                    t2 = new Vector2(1 - (floati / l1 * TexcoordMult1), 1 - ((floatj + 1.0f) / l2 * TexcoordMult2));
                    t3 = new Vector2(1 - ((floati + 1.0f) / l1 * TexcoordMult1), 1 - (floatj / l2 * TexcoordMult2));
                    t4 = new Vector2(1 - ((floati + 1.0f) / l1 * TexcoordMult1), 1 - ((floatj + 1.0f) / l2 * TexcoordMult2));
                    Vector3 tan1, tan2, tan3, tan4;
                    //Tangent in the direction of the U coordinates
                    tan1 = Vector3.Normalize(n1 - n3);
                    tan2 = Vector3.Normalize(n2 - n4);
                    tan3 = tan1;
                    tan4 = tan2;
                    Vector3 bin1, bin2, bin3, bin4;
                    //Binormal in the direction of the V coordinates.
                    bin1 = Vector3.Normalize(n1 - n2);
                    bin2 = bin1;
                    bin3 = Vector3.Normalize(n3 - n4);
                    bin4 = bin3;

                    cubeVertices[vertIndex] = new Game1.JelloVertexFormat(n1, normal, color, t1, tan1, bin1);
                    cubeVertices[vertIndex + 1] = new Game1.JelloVertexFormat(n3, normal, color, t3, tan3, bin3);
                    cubeVertices[vertIndex + 2] = new Game1.JelloVertexFormat(n2, normal, color, t2, tan2, bin2);

                    normal = Vector3.Cross(n3 - n4, n2 - n4);
                    cubeVertices[vertIndex + 3] = new Game1.JelloVertexFormat(n2, normal, color, t2, tan2, bin2);
                    cubeVertices[vertIndex + 4] = new Game1.JelloVertexFormat(n3, normal, color, t3, tan3, bin3);
                    cubeVertices[vertIndex + 5] = new Game1.JelloVertexFormat(n4, normal, color, t4, tan4, bin4);
                    vertIndex += 6;
                }

            //Set verts side 2opp
            for (int j = 0; j < cubeNodes.GetLength(1) - 1; j++)
                for (int k = 0; k < cubeNodes.GetLength(2) - 1; k++)
                {
                    int i = cubeNodes.GetLength(0) - 1;
                    Vector3 n1, n2, n3, n4;
                    n1 = cubeNodes[i, j, k].Position;
                    n2 = cubeNodes[i, j, k + 1].Position;
                    n3 = cubeNodes[i, j + 1, k].Position;
                    n4 = cubeNodes[i, j + 1, k + 1].Position;
                    Vector3 normal = Vector3.Cross(n3 - n1, n2 - n1);
                    Vector2 t1, t2, t3, t4;
                    float floati = (float)j; float floatj = (float)k;
                    float l1 = (float)(cubeNodes.GetLength(1) - 1); float l2 = (cubeNodes.GetLength(2) - 1);
                    float TexcoordMult1 = l1 / unitsPerTexCoord * yEdgeLength;
                    float TexcoordMult2 = l2 / unitsPerTexCoord * zEdgeLength;
                    t1 = new Vector2(floati / l1 * TexcoordMult1, 1 - (floatj / l2 * TexcoordMult2));
                    t2 = new Vector2(floati / l1 * TexcoordMult1, 1 - ((floatj + 1.0f) / l2 * TexcoordMult2));
                    t3 = new Vector2((floati + 1.0f) / l1 * TexcoordMult1, 1 - (floatj / l2 * TexcoordMult2));
                    t4 = new Vector2((floati + 1.0f) / l1 * TexcoordMult1, 1 - ((floatj + 1.0f) / l2 * TexcoordMult2));
                    Vector3 tan1, tan2, tan3, tan4;
                    //Tangent in the direction of the U coordinates
                    tan1 = Vector3.Normalize(n3 - n1);
                    tan2 = Vector3.Normalize(n4 - n2);
                    tan3 = tan1;
                    tan4 = tan2;
                    Vector3 bin1, bin2, bin3, bin4;
                    //Binormal in the direction of the V coordinates.
                    bin1 = Vector3.Normalize(n1 - n2);
                    bin2 = bin1;
                    bin3 = Vector3.Normalize(n3 - n4);
                    bin4 = bin3;

                    cubeVertices[vertIndex] = new Game1.JelloVertexFormat(n1, normal, color, t1, tan1, bin1);
                    cubeVertices[vertIndex + 1] = new Game1.JelloVertexFormat(n2, normal, color, t2, tan2, bin2);
                    cubeVertices[vertIndex + 2] = new Game1.JelloVertexFormat(n3, normal, color, t3, tan3, bin3);

                    normal = Vector3.Cross(n2 - n4, n3 - n4);
                    cubeVertices[vertIndex + 3] = new Game1.JelloVertexFormat(n2, normal, color, t2, tan2, bin2);
                    cubeVertices[vertIndex + 4] = new Game1.JelloVertexFormat(n4, normal, color, t4, tan4, bin4);
                    cubeVertices[vertIndex + 5] = new Game1.JelloVertexFormat(n3, normal, color, t3, tan3, bin3);
                    vertIndex += 6;
                }

            //3
            for (int k = 0; k < cubeNodes.GetLength(2) - 1; k++)
                for (int i = 0; i < cubeNodes.GetLength(0) - 1; i++)
                {
                    int j = 0;
                    Vector3 n1, n2, n3, n4;
                    n1 = cubeNodes[i, j, k].Position;
                    n2 = cubeNodes[i + 1, j, k].Position;
                    n3 = cubeNodes[i, j, k + 1].Position;
                    n4 = cubeNodes[i + 1, j, k + 1].Position;
                    Vector3 normal = Vector3.Cross(n2 - n1, n3 - n1);
                    Vector2 t1, t2, t3, t4;
                    float floati = (float)k; float floatj = (float)i;
                    float l1 = (float)(cubeNodes.GetLength(2) - 1); float l2 = (cubeNodes.GetLength(0) - 1);
                    float TexcoordMult1 = l1 / unitsPerTexCoord * zEdgeLength;
                    float TexcoordMult2 = l2 / unitsPerTexCoord * xEdgeLength;
                    t1 = new Vector2(floati / l1 * TexcoordMult1, floatj / l2 * TexcoordMult2);
                    t2 = new Vector2(floati / l1 * TexcoordMult1, (floatj + 1.0f) / l2 * TexcoordMult2);
                    t3 = new Vector2((floati + 1.0f) / l1 * TexcoordMult1, floatj / l2 * TexcoordMult2);
                    t4 = new Vector2((floati + 1.0f) / l1 * TexcoordMult1, (floatj + 1.0f) / l2 * TexcoordMult2);
                    Vector3 tan1, tan2, tan3, tan4;
                    //Tangent in the direction of the U coordinates
                    tan1 = Vector3.Normalize(n3 - n1);
                    tan2 = Vector3.Normalize(n4 - n2);
                    tan3 = tan1;
                    tan4 = tan2;
                    Vector3 bin1, bin2, bin3, bin4;
                    //Binormal in the direction of the V coordinates.
                    bin1 = Vector3.Normalize(n2 - n1);
                    bin2 = bin1;
                    bin3 = Vector3.Normalize(n4 - n3);
                    bin4 = bin3;
                    cubeVertices[vertIndex] = new Game1.JelloVertexFormat(n1, normal, color, t1, tan1, bin1);
                    cubeVertices[vertIndex + 1] = new Game1.JelloVertexFormat(n3, normal, color, t3, tan3, bin3);
                    cubeVertices[vertIndex + 2] = new Game1.JelloVertexFormat(n2, normal, color, t2, tan2, bin2);

                    normal = Vector3.Cross(n3 - n4, n2 - n4);
                    cubeVertices[vertIndex + 3] = new Game1.JelloVertexFormat(n2, normal, color, t2, tan2, bin2);
                    cubeVertices[vertIndex + 4] = new Game1.JelloVertexFormat(n3, normal, color, t3, tan3, bin3);
                    cubeVertices[vertIndex + 5] = new Game1.JelloVertexFormat(n4, normal, color, t4, tan4, bin4);
                    vertIndex += 6;
                }

            //Set verts side 3opp
            for (int k = 0; k < cubeNodes.GetLength(2) - 1; k++)
                for (int i = 0; i < cubeNodes.GetLength(0) - 1; i++)
                {
                    int j = cubeNodes.GetLength(1) - 1;
                    Vector3 n1, n2, n3, n4;
                    n1 = cubeNodes[i, j, k].Position;
                    n2 = cubeNodes[i + 1, j, k].Position;
                    n3 = cubeNodes[i, j, k + 1].Position;
                    n4 = cubeNodes[i + 1, j, k + 1].Position;
                    Vector3 normal = Vector3.Cross(n3 - n1, n2 - n1);
                    Vector2 t1, t2, t3, t4;
                    float floati = (float)k; float floatj = (float)i;
                    float l1 = (float)(cubeNodes.GetLength(2) - 1); float l2 = (cubeNodes.GetLength(0) - 1);
                    float TexcoordMult1 = l1 / unitsPerTexCoord * zEdgeLength;
                    float TexcoordMult2 = l2 / unitsPerTexCoord * xEdgeLength;
                    t1 = new Vector2(floati / l1 * TexcoordMult1, 1 - (floatj / l2 * TexcoordMult2));
                    t2 = new Vector2(floati / l1 * TexcoordMult1, 1 - ((floatj + 1.0f) / l2 * TexcoordMult2));
                    t3 = new Vector2((floati + 1.0f) / l1 * TexcoordMult1, 1 - (floatj / l2 * TexcoordMult2));
                    t4 = new Vector2((floati + 1.0f) / l1 * TexcoordMult1, 1 - ((floatj + 1.0f) / l2 * TexcoordMult2));
                    Vector3 tan1, tan2, tan3, tan4;
                    //Tangent in the direction of the U coordinates
                    tan1 = Vector3.Normalize(n3 - n1);
                    tan2 = Vector3.Normalize(n4 - n2);
                    tan3 = tan1;
                    tan4 = tan2;
                    Vector3 bin1, bin2, bin3, bin4;
                    //Binormal in the direction of the V coordinates.
                    bin1 = Vector3.Normalize(n1 - n2);
                    bin2 = bin1;
                    bin3 = Vector3.Normalize(n3 - n4);
                    bin4 = bin3;
                    cubeVertices[vertIndex] = new Game1.JelloVertexFormat(n1, normal, color, t1, tan1, bin1);
                    cubeVertices[vertIndex + 1] = new Game1.JelloVertexFormat(n2, normal, color, t2, tan2, bin2);
                    cubeVertices[vertIndex + 2] = new Game1.JelloVertexFormat(n3, normal, color, t3, tan3, bin3);

                    normal = Vector3.Cross(n2 - n4, n3 - n4);
                    cubeVertices[vertIndex + 3] = new Game1.JelloVertexFormat(n2, normal, color, t2, tan2, bin2);
                    cubeVertices[vertIndex + 4] = new Game1.JelloVertexFormat(n4, normal, color, t4, tan4, bin4);
                    cubeVertices[vertIndex + 5] = new Game1.JelloVertexFormat(n3, normal, color, t3, tan3, bin3);
                    vertIndex += 6;
                }
        }

        public void Update()
        {
            int vertIndex = 0;
            for (int i = 0; i < cubeNodes.GetLength(0) - 1; i++)
                for (int j = 0; j < cubeNodes.GetLength(1) - 1; j++)
                {
                    int k = 0;
                    cubeVertices[vertIndex].Position = cubeNodes[i, j, k].Position;
                    cubeVertices[vertIndex + 1].Position = cubeNodes[i + 1, j, k].Position;
                    cubeVertices[vertIndex + 2].Position = cubeNodes[i, j + 1, k].Position;
                    cubeVertices[vertIndex + 3].Position = cubeNodes[i, j + 1, k].Position;
                    cubeVertices[vertIndex + 4].Position = cubeNodes[i + 1, j, k].Position;
                    cubeVertices[vertIndex + 5].Position = cubeNodes[i + 1, j + 1, k].Position;
                    vertIndex += 6;
                }

            //Set verts side 1opp
            for (int i = 0; i < cubeNodes.GetLength(0) - 1; i++)
                for (int j = 0; j < cubeNodes.GetLength(1) - 1; j++)
                {
                    int k = cubeNodes.GetLength(2) - 1;
                    cubeVertices[vertIndex].Position = cubeNodes[i, j, k].Position;
                    cubeVertices[vertIndex + 1].Position = cubeNodes[i, j + 1, k].Position;
                    cubeVertices[vertIndex + 2].Position = cubeNodes[i + 1, j, k].Position;
                    cubeVertices[vertIndex + 3].Position = cubeNodes[i + 1, j, k].Position;
                    cubeVertices[vertIndex + 4].Position = cubeNodes[i, j + 1, k].Position;
                    cubeVertices[vertIndex + 5].Position = cubeNodes[i + 1, j + 1, k].Position;
                    vertIndex += 6;
                }

            //2
            for (int j = 0; j < cubeNodes.GetLength(1) - 1; j++)
                for (int k = 0; k < cubeNodes.GetLength(2) - 1; k++)
                {
                    int i = 0;
                    cubeVertices[vertIndex].Position = cubeNodes[i, j, k].Position;
                    cubeVertices[vertIndex + 1].Position = cubeNodes[i, j + 1, k].Position;
                    cubeVertices[vertIndex + 2].Position = cubeNodes[i, j, k + 1].Position;
                    cubeVertices[vertIndex + 3].Position = cubeNodes[i, j, k + 1].Position;
                    cubeVertices[vertIndex + 4].Position = cubeNodes[i, j + 1, k].Position;
                    cubeVertices[vertIndex + 5].Position = cubeNodes[i, j + 1, k + 1].Position;
                    vertIndex += 6;
                }

            //Set verts side 2opp
            for (int j = 0; j < cubeNodes.GetLength(1) - 1; j++)
                for (int k = 0; k < cubeNodes.GetLength(2) - 1; k++)
                {
                    int i = cubeNodes.GetLength(0) - 1;
                    cubeVertices[vertIndex].Position = cubeNodes[i, j, k].Position;
                    cubeVertices[vertIndex + 1].Position = cubeNodes[i, j, k + 1].Position;
                    cubeVertices[vertIndex + 2].Position = cubeNodes[i, j + 1, k].Position;
                    cubeVertices[vertIndex + 3].Position = cubeNodes[i, j, k + 1].Position;
                    cubeVertices[vertIndex + 4].Position = cubeNodes[i, j + 1, k + 1].Position;
                    cubeVertices[vertIndex + 5].Position = cubeNodes[i, j + 1, k].Position;
                    vertIndex += 6;
                }
            //3
            for (int k = 0; k < cubeNodes.GetLength(2) - 1; k++)
                for (int i = 0; i < cubeNodes.GetLength(0) - 1; i++)
                {
                    int j = 0;
                    cubeVertices[vertIndex].Position = cubeNodes[i, j, k].Position;
                    cubeVertices[vertIndex + 1].Position = cubeNodes[i, j, k + 1].Position;
                    cubeVertices[vertIndex + 2].Position = cubeNodes[i + 1, j, k].Position;
                    cubeVertices[vertIndex + 3].Position = cubeNodes[i + 1, j, k].Position;
                    cubeVertices[vertIndex + 4].Position = cubeNodes[i, j, k + 1].Position;
                    cubeVertices[vertIndex + 5].Position = cubeNodes[i + 1, j, k + 1].Position;
                    vertIndex += 6;
                }

            //Set verts side 3opp
            for (int k = 0; k < cubeNodes.GetLength(2) - 1; k++)
                for (int i = 0; i < cubeNodes.GetLength(0) - 1; i++)
                {
                    int j = cubeNodes.GetLength(1) - 1;
                    cubeVertices[vertIndex].Position = cubeNodes[i, j, k].Position;
                    cubeVertices[vertIndex + 1].Position = cubeNodes[i + 1, j, k].Position;
                    cubeVertices[vertIndex + 2].Position = cubeNodes[i, j, k + 1].Position;
                    cubeVertices[vertIndex + 3].Position = cubeNodes[i + 1, j, k].Position;
                    cubeVertices[vertIndex + 4].Position = cubeNodes[i + 1, j, k + 1].Position;
                    cubeVertices[vertIndex + 5].Position = cubeNodes[i, j, k + 1].Position;
                    vertIndex += 6;
                }

            vertIndex = 0;
            for (int i = 0; i < cubeNodes.GetLength(0) - 1; i++)
                for (int j = 0; j < cubeNodes.GetLength(1) - 1; j++)
                {
                    int k = 0;
                    Vector3 n1, n2, n3, n4;
                    n1 = cubeNodes[i, j, k].Position;
                    n2 = cubeNodes[i, j + 1, k].Position;
                    n3 = cubeNodes[i + 1, j, k].Position;
                    n4 = cubeNodes[i + 1, j + 1, k].Position;

                    Vector3 tan1, tan2, tan3, tan4;
                    //Tangent in the direction of the U coordinates
                    tan1 = Vector3.Normalize(n3 - n1);
                    tan2 = Vector3.Normalize(n4 - n2);
                    tan3 = tan1;
                    tan4 = tan2;
                    Vector3 bin1, bin2, bin3, bin4;
                    //Binormal in the direction of the V coordinates.
                    bin1 = Vector3.Normalize(n2 - n1);
                    bin2 = bin1;
                    bin3 = Vector3.Normalize(n4 - n3);
                    bin4 = bin3;

                    Vector3 normal = Vector3.Cross(n2 - n1, n3 - n1);
                    cubeVertices[vertIndex].Normal = normal;
                    cubeVertices[vertIndex].Tangent = tan1;
                    cubeVertices[vertIndex].Binormal = bin1;
                    cubeVertices[vertIndex + 1].Normal = normal;
                    cubeVertices[vertIndex + 1].Tangent = tan2;
                    cubeVertices[vertIndex + 1].Binormal = bin2;
                    cubeVertices[vertIndex + 2].Normal = normal;
                    cubeVertices[vertIndex + 2].Tangent = tan3;
                    cubeVertices[vertIndex + 2].Binormal = bin3;

                    normal = Vector3.Cross(n3 - n4, n2 - n4);
                    cubeVertices[vertIndex + 3].Normal = normal;
                    cubeVertices[vertIndex + 3].Tangent = tan2;
                    cubeVertices[vertIndex + 3].Binormal = bin2;
                    cubeVertices[vertIndex + 4].Normal = normal;
                    cubeVertices[vertIndex + 4].Tangent = tan3;
                    cubeVertices[vertIndex + 4].Binormal = bin3;
                    cubeVertices[vertIndex + 5].Normal = normal;
                    cubeVertices[vertIndex + 5].Tangent = tan4;
                    cubeVertices[vertIndex + 5].Binormal = bin4;
                    vertIndex += 6;
                }

            //Set verts side 1opp
            for (int i = 0; i < cubeNodes.GetLength(0) - 1; i++)
                for (int j = 0; j < cubeNodes.GetLength(1) - 1; j++)
                {
                    int k = cubeNodes.GetLength(2) - 1;
                    Vector3 n1, n2, n3, n4;
                    n1 = cubeNodes[i, j, k].Position;
                    n2 = cubeNodes[i, j + 1, k].Position;
                    n3 = cubeNodes[i + 1, j, k].Position;
                    n4 = cubeNodes[i + 1, j + 1, k].Position;
                    Vector3 tan1, tan2, tan3, tan4;
                    //Tangent in the direction of the U coordinates
                    tan1 = Vector3.Normalize(n3 - n1);
                    tan2 = Vector3.Normalize(n4 - n2);
                    tan3 = tan1;
                    tan4 = tan2;
                    Vector3 bin1, bin2, bin3, bin4;
                    //Binormal in the direction of the V coordinates.
                    bin1 = Vector3.Normalize(n2 - n1);
                    bin2 = bin1;
                    bin3 = Vector3.Normalize(n4 - n3);
                    bin4 = bin3;

                    Vector3 normal = Vector3.Cross(n3 - n1, n2 - n1);
                    cubeVertices[vertIndex].Normal = normal;
                    cubeVertices[vertIndex].Tangent = tan1;
                    cubeVertices[vertIndex].Binormal = bin1;
                    cubeVertices[vertIndex + 1].Normal = normal;
                    cubeVertices[vertIndex + 1].Tangent = tan2;
                    cubeVertices[vertIndex + 1].Binormal = bin2;
                    cubeVertices[vertIndex + 2].Normal = normal;
                    cubeVertices[vertIndex + 2].Tangent = tan3;
                    cubeVertices[vertIndex + 2].Binormal = bin3;

                    normal = Vector3.Cross(n2 - n4, n3 - n4);
                    cubeVertices[vertIndex + 3].Normal = normal;
                    cubeVertices[vertIndex + 3].Tangent = tan3;
                    cubeVertices[vertIndex + 3].Binormal = bin3;
                    cubeVertices[vertIndex + 4].Normal = normal;
                    cubeVertices[vertIndex + 4].Tangent = tan2;
                    cubeVertices[vertIndex + 4].Binormal = bin2;
                    cubeVertices[vertIndex + 5].Normal = normal;
                    cubeVertices[vertIndex + 5].Tangent = tan4;
                    cubeVertices[vertIndex + 5].Binormal = bin4;
                    vertIndex += 6;
                }

            //2
            for (int j = 0; j < cubeNodes.GetLength(1) - 1; j++)
                for (int k = 0; k < cubeNodes.GetLength(2) - 1; k++)
                {
                    int i = 0;
                    Vector3 n1, n2, n3, n4;
                    n1 = cubeNodes[i, j, k].Position;
                    n2 = cubeNodes[i, j, k + 1].Position;
                    n3 = cubeNodes[i, j + 1, k].Position;
                    n4 = cubeNodes[i, j + 1, k + 1].Position;
                    Vector3 tan1, tan2, tan3, tan4;
                    //Tangent in the direction of the U coordinates
                    tan1 = Vector3.Normalize(n1 - n3);
                    tan2 = Vector3.Normalize(n2 - n4);
                    tan3 = tan1;
                    tan4 = tan2;
                    Vector3 bin1, bin2, bin3, bin4;
                    //Binormal in the direction of the V coordinates.
                    bin1 = Vector3.Normalize(n1 - n2);
                    bin2 = bin1;
                    bin3 = Vector3.Normalize(n3 - n4);
                    bin4 = bin3;
                    Vector3 normal = Vector3.Cross(n2 - n1, n3 - n1);
                    cubeVertices[vertIndex].Normal = normal;
                    cubeVertices[vertIndex].Tangent = tan1;
                    cubeVertices[vertIndex].Binormal = bin1;
                    cubeVertices[vertIndex + 1].Normal = normal;
                    cubeVertices[vertIndex + 1].Tangent = tan3;
                    cubeVertices[vertIndex + 1].Binormal = bin3;
                    cubeVertices[vertIndex + 2].Normal = normal;
                    cubeVertices[vertIndex + 2].Tangent = tan2;
                    cubeVertices[vertIndex + 2].Binormal = bin2;

                    normal = Vector3.Cross(n3 - n4, n2 - n4);
                    cubeVertices[vertIndex + 3].Normal = normal;
                    cubeVertices[vertIndex + 3].Tangent = tan2;
                    cubeVertices[vertIndex + 3].Binormal = bin2;
                    cubeVertices[vertIndex + 4].Normal = normal;
                    cubeVertices[vertIndex + 4].Tangent = tan3;
                    cubeVertices[vertIndex + 4].Binormal = bin3;
                    cubeVertices[vertIndex + 5].Normal = normal;
                    cubeVertices[vertIndex + 5].Tangent = tan4;
                    cubeVertices[vertIndex + 5].Binormal = bin4;
                    vertIndex += 6;
                }

            //Set verts side 2opp
            for (int j = 0; j < cubeNodes.GetLength(1) - 1; j++)
                for (int k = 0; k < cubeNodes.GetLength(2) - 1; k++)
                {
                    int i = cubeNodes.GetLength(0) - 1;
                    Vector3 n1, n2, n3, n4;
                    n1 = cubeNodes[i, j, k].Position;
                    n2 = cubeNodes[i, j, k + 1].Position;
                    n3 = cubeNodes[i, j + 1, k].Position;
                    n4 = cubeNodes[i, j + 1, k + 1].Position;
                    Vector3 tan1, tan2, tan3, tan4;
                    //Tangent in the direction of the U coordinates
                    tan1 = Vector3.Normalize(n3 - n1);
                    tan2 = Vector3.Normalize(n4 - n2);
                    tan3 = tan1;
                    tan4 = tan2;
                    Vector3 bin1, bin2, bin3, bin4;
                    //Binormal in the direction of the V coordinates.
                    bin1 = Vector3.Normalize(n1 - n2);
                    bin2 = bin1;
                    bin3 = Vector3.Normalize(n3 - n4);
                    bin4 = bin3;
                    Vector3 normal = Vector3.Cross(n3 - n1, n2 - n1);
                    cubeVertices[vertIndex].Normal = normal;
                    cubeVertices[vertIndex].Tangent = tan1;
                    cubeVertices[vertIndex].Binormal = bin1;
                    cubeVertices[vertIndex + 1].Normal = normal;
                    cubeVertices[vertIndex + 1].Tangent = tan2;
                    cubeVertices[vertIndex + 1].Binormal = bin2;
                    cubeVertices[vertIndex + 2].Normal = normal;
                    cubeVertices[vertIndex + 2].Tangent = tan3;
                    cubeVertices[vertIndex + 2].Binormal = bin3;

                    normal = Vector3.Cross(n2 - n4, n3 - n4);
                    cubeVertices[vertIndex + 3].Normal = normal;
                    cubeVertices[vertIndex + 3].Tangent = tan2;
                    cubeVertices[vertIndex + 3].Binormal = bin2;
                    cubeVertices[vertIndex + 4].Normal = normal;
                    cubeVertices[vertIndex + 4].Tangent = tan4;
                    cubeVertices[vertIndex + 4].Binormal = bin4;
                    cubeVertices[vertIndex + 5].Normal = normal;
                    cubeVertices[vertIndex + 5].Tangent = tan3;
                    cubeVertices[vertIndex + 5].Binormal = bin3;
                    vertIndex += 6;
                }

            //3
            for (int k = 0; k < cubeNodes.GetLength(2) - 1; k++)
                for (int i = 0; i < cubeNodes.GetLength(0) - 1; i++)
                {
                    int j = 0;
                    Vector3 n1, n2, n3, n4;
                    n1 = cubeNodes[i, j, k].Position;
                    n2 = cubeNodes[i + 1, j, k].Position;
                    n3 = cubeNodes[i, j, k + 1].Position;
                    n4 = cubeNodes[i + 1, j, k + 1].Position;
                    Vector3 tan1, tan2, tan3, tan4;
                    //Tangent in the direction of the U coordinates
                    tan1 = Vector3.Normalize(n3 - n1);
                    tan2 = Vector3.Normalize(n4 - n2);
                    tan3 = tan1;
                    tan4 = tan2;
                    Vector3 bin1, bin2, bin3, bin4;
                    //Binormal in the direction of the V coordinates.
                    bin1 = Vector3.Normalize(n2 - n1);
                    bin2 = bin1;
                    bin3 = Vector3.Normalize(n4 - n3);
                    bin4 = bin3;
                    Vector3 normal = Vector3.Cross(n2 - n1, n3 - n1);
                    cubeVertices[vertIndex].Normal = normal;
                    cubeVertices[vertIndex].Tangent = tan1;
                    cubeVertices[vertIndex].Binormal = bin1;
                    cubeVertices[vertIndex + 1].Normal = normal;
                    cubeVertices[vertIndex + 1].Tangent = tan3;
                    cubeVertices[vertIndex + 1].Binormal = bin3;
                    cubeVertices[vertIndex + 2].Normal = normal;
                    cubeVertices[vertIndex + 2].Tangent = tan2;
                    cubeVertices[vertIndex + 2].Binormal = bin2;

                    normal = Vector3.Cross(n3 - n4, n2 - n4);
                    cubeVertices[vertIndex + 3].Normal = normal;
                    cubeVertices[vertIndex + 3].Tangent = tan2;
                    cubeVertices[vertIndex + 3].Binormal = bin2;
                    cubeVertices[vertIndex + 4].Normal = normal;
                    cubeVertices[vertIndex + 4].Tangent = tan3;
                    cubeVertices[vertIndex + 4].Binormal = bin3;
                    cubeVertices[vertIndex + 5].Normal = normal;
                    cubeVertices[vertIndex + 5].Tangent = tan4;
                    cubeVertices[vertIndex + 5].Binormal = bin4;
                    vertIndex += 6;
                }

            //Set verts side 3opp
            for (int k = 0; k < cubeNodes.GetLength(2) - 1; k++)
                for (int i = 0; i < cubeNodes.GetLength(0) - 1; i++)
                {
                    int j = cubeNodes.GetLength(1) - 1;
                    Vector3 n1, n2, n3, n4;
                    n1 = cubeNodes[i, j, k].Position;
                    n2 = cubeNodes[i + 1, j, k].Position;
                    n3 = cubeNodes[i, j, k + 1].Position;
                    n4 = cubeNodes[i + 1, j, k + 1].Position;
                    Vector3 tan1, tan2, tan3, tan4;
                    //Tangent in the direction of the U coordinates
                    tan1 = Vector3.Normalize(n3 - n1);
                    tan2 = Vector3.Normalize(n4 - n2);
                    tan3 = tan1;
                    tan4 = tan2;
                    Vector3 bin1, bin2, bin3, bin4;
                    //Binormal in the direction of the V coordinates.
                    bin1 = Vector3.Normalize(n1 - n2);
                    bin2 = bin1;
                    bin3 = Vector3.Normalize(n3 - n4);
                    bin4 = bin3;
                    Vector3 normal = Vector3.Cross(n3 - n1, n2 - n1);
                    cubeVertices[vertIndex].Normal = normal;
                    cubeVertices[vertIndex].Tangent = tan1;
                    cubeVertices[vertIndex].Binormal = bin1;
                    cubeVertices[vertIndex + 1].Normal = normal;
                    cubeVertices[vertIndex + 1].Tangent = tan2;
                    cubeVertices[vertIndex + 1].Binormal = bin2;
                    cubeVertices[vertIndex + 2].Normal = normal;
                    cubeVertices[vertIndex + 2].Tangent = tan3;
                    cubeVertices[vertIndex + 2].Binormal = bin3;

                    normal = Vector3.Cross(n2 - n4, n3 - n4);
                    cubeVertices[vertIndex + 3].Normal = normal;
                    cubeVertices[vertIndex + 3].Tangent = tan2;
                    cubeVertices[vertIndex + 3].Binormal = bin2;
                    cubeVertices[vertIndex + 4].Normal = normal;
                    cubeVertices[vertIndex + 4].Tangent = tan4;
                    cubeVertices[vertIndex + 4].Binormal = bin4;
                    cubeVertices[vertIndex + 5].Normal = normal;
                    cubeVertices[vertIndex + 5].Tangent = tan3;
                    cubeVertices[vertIndex + 5].Binormal = bin3;
                    vertIndex += 6;
                }
        }

        public int CompareTo(JelloCube obj)
        {
            //@TODO: Use the closest of the edge points to the camera
            if ((this.cubeNodes[0, 0, 0].Position - game.camera.CameraPosition).Length() < (obj.cubeNodes[0, 0, 0].Position - game.camera.CameraPosition).Length())
                return 1;
            else if ((this.cubeNodes[0, 0, 0].Position - game.camera.CameraPosition).Length() == (obj.cubeNodes[0, 0, 0].Position - game.camera.CameraPosition).Length())
                return 0;
            else
                return -1;
        }
    }
}
